package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.ExtraBaseQuestion;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.IncludeReplyBottomBinding;
import com.jwdf.uda.databinding.IncludeReplyTopBinding;
import com.jwdf.uda.databinding.ItemReplyTxtBinding;
import com.jwdf.uda.ui.activity.ReplyDetaiActivtiy;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2016/9/7.
 */
public class QuizReplyTxtViewHolder extends AbsViewHolder<QuizDetailItem> {
    private ItemReplyTxtBinding mItemBinding;

    public QuizReplyTxtViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);
        convertView.setOnClickListener(this);
        mItemBinding.topLayout.llHeadLayout.setOnClickListener(this);
    }

    static void onBindCommonView(IncludeReplyTopBinding topLayout, IncludeReplyBottomBinding bottomLayout, PbBean.PbBaseQuestionAnswer reply) {
        PbBean.PbUserSimple userInfo = reply.getUserInfo();
        topLayout.tvName.setText(userInfo.getNickname());
        topLayout.tvIntro.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(userInfo.getIntroduction())) ? View.GONE : View.VISIBLE);
        topLayout.tvIntro.setText(userInfo.getIntroduction());
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), topLayout.ivHeader);
        topLayout.setIsDesigner(userInfo.getIsDesigner());
        if (reply.getVoteOption() != null) {
            switch (reply.getVoteOption()) {
                case VOTE_OPTION_TYPE_A:
                    topLayout.tvVoteOption.setText("Ta选了A");
                    topLayout.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_B:
                    topLayout.tvVoteOption.setText("Ta选了B");
                    topLayout.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_C:
                    topLayout.tvVoteOption.setText("Ta选了C");
                    topLayout.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_D:
                    topLayout.tvVoteOption.setText("Ta选了D");
                    topLayout.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_E:
                    topLayout.tvVoteOption.setText("Ta选了E");
                    topLayout.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_NONE:
                    topLayout.tvVoteOption.setVisibility(View.GONE);
                    break;
            }
        } else {
            topLayout.tvVoteOption.setVisibility(View.GONE);
        }
        topLayout.tvContent.setVisibility(!TextUtils.isEmpty(StringUtils.filterNullAndTrim(reply.getQuickDescription())) ? View.VISIBLE : View.GONE);
        topLayout.tvContent.setText(reply.getQuickDescription());
        bottomLayout.tvDate.setText(DateUtils.getFriendlyTime(reply.getCreateTime()));
        bottomLayout.tvCommentZan.setText(bottomLayout.getRoot().getContext().getString(R.string.reply_comment_like, reply.getCommentCount(), reply.getLikeCount()));
    }

    @Override
    protected void onBindView(QuizDetailItem item) {
        PbBean.PbBaseQuestionAnswer reply = item.reply;
        if (reply == null) return;
        onBindCommonView(mItemBinding.topLayout, mItemBinding.bottomLayout, reply);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_head_layout) {
            UserInfoActivity.start(v.getContext(), item.reply.getUserInfo().getUserId(), item.reply.getUserInfo().getIsDesigner());
            return;
        }
        boolean isAuthor = TextUtils.equals(item.reply.getUserInfo().getUserId(), ConfigUtils.getUserId(v.getContext()));
        ReplyDetaiActivtiy.start(v.getContext(), item.reply.getAnswerId(), isAuthor, item.planVoteCount, new ExtraBaseQuestion());
    }
}
